<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/4
 * Time: 14:04
 */

namespace App\Http\Admin\Administration\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\Administration\Models\OaNotice;
use Framework\Extend\Helpers\Upload;
use Intervention\Image\Facades\Image;

class OaNoticeController extends Controller
{


    /**
     * 发送的通知列表
     * @Author sxy
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function sendOaNoticeList()
    {
        if (request()->isMethod('post')) {
            // return request()->all();
            $page = request('pageCurrent');
            $pageSize = request('pageSize');
            $search = request()->except(['pageCurrent', 'pageSize']);
            $searchField = [];
            $oa_company_id = session('current_operation_company_id', 1);
            $searchField[] = ['oa_company_id', '=', $oa_company_id];
            $user = request()->user('admin');
            $searchField[] = ['publisher_id', '=', $user['id']];


            if (!empty($search)) {
                if (isset($search['start_time'])) $searchField[] = ['release_time', ">=", strtotime($search['start_time'])];
                if (isset($search['end_time'])) $searchField[] = ['release_time', "<=", strtotime($search['end_time'])];
                unset($search['start_time']);
                unset($search['end_time']);
                if (!empty($search)) {
                    foreach ($search as $k => $v) {
                        if ($v || $v == "0") $searchField[] = [$k, 'like', '%' . $v . '%'];
                    }
                }
            }

            $oaNotice = new OaNotice();
            return $oaNotice->getSendNoticeList($page, $pageSize, $searchField);
        } else {
            $oaNotice = new OaNotice();
            $releaseScope = $oaNotice->getReleaseScope();
            return $this->view('send-notice-list')->with('releasescope', $releaseScope);
        }
    }

    /**
     * @Author sxy
     * 发送通知公告
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function sendOaNoticeAdd()
    {
        try {
            if (request()->isMethod('post')) {
                $request = request()->all();
                $file = request()->file('file');
                if ($file) {
                    $fileInfo = Upload::uploadFiles($file, 'oa-notice-file');
                    if ($fileInfo) {
                        $request['attachment_url'] = '/' . $fileInfo['fullPath'];
                    }
                }
                $oa_company_id = session('current_operation_company_id', 1);
                $request['oa_company_id'] = $oa_company_id;
                $user = request()->user('admin');
                $request['publisher_id'] = $user['id'];
                if(!is_null($request['oa_contacts_id'])) {
                    $oa_contacts_id = explode(',',$request['oa_contacts_id']);//todo 通讯录id

                }else{
                    $oa_contacts_id=[];
                }


                if(!is_null($request['oa_department_id'])) {
                    $oaNotice = new OaNotice();
                    $request['oa_department_id'] = explode(',', $request['oa_department_id']);

                    $contacts_id = $oaNotice->getContactsId($request['oa_department_id']);
                    $oa_department_id = $contacts_id;//todo 通讯录id

                }else{
                    $oa_department_id=[];
                }

                $request['address_name']=array_unique(array_merge($oa_department_id,$oa_contacts_id));

                $rules = [
                    'title' => 'required',
                    //'address_name' => 'required',todo 通讯录
                    'content' => 'required',
                    'key_word' => 'required',
                ];
                $this->validateRequestParameters($request, $rules, true);
                $oaNotice = new OaNotice();
                $re = $oaNotice->addSendNotice($request);
                if (!$re) return $this->ajaxFail();
                return $this->ajaxSuccess();

            } else {

                return $this->view('send-notice-add');
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }

    /**
     * @Author sxy
     * 编辑发送的通知公告
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function sendOaNoticeEdit()
    {
        try {
            if (request()->isMethod('post')) {

                $request = request()->all();
                $rules = [
                    'title' => 'required',
                    'content' => 'required',
                    'key_word' => 'required',
                    'release_time' => 'required',
                ];



                if(!is_null($request['oa_contacts_id'])) {
                    $oa_contacts_id = explode(',',$request['oa_contacts_id']);//todo 通讯录id

                }else{
                    $oa_contacts_id=[];
                }


                if(!is_null($request['oa_department_id'])) {
                    $oaNotice = new OaNotice();
                    $request['oa_department_id']=explode(',',$request['oa_department_id']);

                    $contacts_id=$oaNotice->getContactsId($request['oa_department_id']);
                    $oa_department_id = $contacts_id;//todo 通讯录id
                }else{
                    $oa_department_id=[];
                }

                $request['address_name']=array_unique(array_merge($oa_department_id,$oa_contacts_id));



                $this->validateRequestParameters($request, $rules, true);
                $oaNotice = new OaNotice();
                $re = $oaNotice->editOaNotice($request);
                if (!$re) return $this->ajaxFail();
                return $this->ajaxSuccess();
            } else {
                $request = request()->all();
                $id = $request['id'];
                $oaNotice = new OaNotice();
                $noticeInfo = $oaNotice->getOaNoticeInfo($id);

                return $this->view('send-notice-edit')->with('noticeInfo', $noticeInfo);
            }

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }


    /**
     * 审核通知列表
     * @Author sxy
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function checkOaNoticeList()
    {
        if (request()->isMethod('post')) {
            $page = request('pageCurrent');
            $pageSize = request('pageSize');
            $search = request()->except(['pageCurrent', 'pageSize']);
            $searchField = [];
            $oa_company_id = session('current_operation_company_id', 1);
            $searchField[] = ['oa_company_id', '=', $oa_company_id];
            $user = request()->user('admin');
            $searchField[] = ['publisher_id', '=', $user['id']];

            if (!empty($search)) {
                if (isset($search['start_time'])) $searchField[] = ['release_time', ">=", strtotime($search['start_time'])];
                if (isset($search['end_time'])) $searchField[] = ['release_time', "<=", strtotime($search['end_time'])];
                unset($search['start_time']);
                unset($search['end_time']);
                if (!empty($search)) {
                    foreach ($search as $k => $v) {
                        if ($v || $v == "0") $searchField[] = [$k, 'like', '%' . $v . '%'];
                    }
                }
            }
            $oaNotice = new OaNotice();
            return $oaNotice->getCheckNoticeList($page, $pageSize, $searchField);
        } else {
            $oaNotice = new OaNotice();
            $releaseScope = $oaNotice->getReleaseScope();
            return $this->view('check-notice-list')->with('releasescope', $releaseScope);
        }
    }


    /**
     * @Author sxy
     * 查看通知公告
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function checkOaNoticeEdit()
    {
        try {
            $request = request()->all();
            $id = $request['id'];
            $oaNotice = new OaNotice();
            $noticeInfo = $oaNotice->getOaNoticeInfo($id);
            return $this->view('check-notice-edit')->with('noticeInfo', $noticeInfo);
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }


    /**
     * @Author sxy
     * 审核通知公告
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function checkOaNoticeCheck()
    {
        try {
            if (request()->isMethod('post')) {

                $request = request()->all();
                $rules = [
                    'is_check' => 'required',
                ];

                $this->validateRequestParameters($request, $rules, true);
                $oaNotice = new OaNotice();
                $re = $oaNotice->CheckNotice($request);
                if (!$re) return $this->ajaxFail();
                return $this->ajaxSuccess();
            } else {
                $id = request('id');
                return $this->view('check-notice-check')->with('id', $id);
            }

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }


    /**
     * 软删除通知公告
     * @Author sxy
     * @return \Illuminate\Http\JsonResponse
     */
    public function delOaNotice()
    {
        try {
            $id = request('id');
            $oaNotice = new OaNotice();
            $re = $oaNotice->delNotice($id);
            if (!$re) return $this->ajaxFail();
            return $this->ajaxSuccess();

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }

    /**
     * 上传通知公告附件
     * @Author sxy
     * @return array
     */
    public function noticeFieUpload()
    {
        $img = request()->file('file');
        try {
            $fileInfo = Upload::uploadFiles($img, 'oa-notice-file');
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

//        $imgRatio = filesize($img) / 1024 / 30;
//        $imagePathArr = explode('/', $fileInfo['fullPath']);
//        $endSub = count($imagePathArr) - 1;
//        $imagePathArr[$endSub] = 'WeChat/' . $imagePathArr[$endSub];
//        $shareImgUrl = implode('/', $imagePathArr);dd($shareImgUrl);
//        $img = Image::make($shareImgUrl)->resize($thumbnailWidth, $thumbnailHeight);
//        $img->save($thumbnailPath, ceil(100 / $imgRatio));
        \DebugBar::disable();
        return [
            'statusCode' => 200,
            'filename' => '/' . $fileInfo['fullPath']
        ];
    }

    /**
     * 上传通知公告图片
     * @Author sxy
     * @return array
     */
    public function noticeImgUpload()
    {
        $img = request()->file('file');
        $fileInfo = Upload::uploadFile($img, 'notice-img');
        $imageSize = getimagesize($fileInfo['fullPath']);

        $thumbnailWidth = round($imageSize[0] / 4);
        $thumbnailHeight = round($imageSize[1] / 4);

        $thumbnailPath = getThumbnailPath($fileInfo['fullPath']);

        Upload::imageThumbnail($fileInfo['fullPath'], $thumbnailPath, $thumbnailWidth, $thumbnailHeight, 20);

//        $imgRatio = filesize($img) / 1024 / 30;
//        $imagePathArr = explode('/', $fileInfo['fullPath']);
//        $endSub = count($imagePathArr) - 1;
//        $imagePathArr[$endSub] = 'WeChat/' . $imagePathArr[$endSub];
//        $shareImgUrl = implode('/', $imagePathArr);dd($shareImgUrl);
//        $img = Image::make($shareImgUrl)->resize($thumbnailWidth, $thumbnailHeight);
//        $img->save($thumbnailPath, ceil(100 / $imgRatio));
        \DebugBar::disable();
        return [
            'statusCode' => 200,
            'filename' => '/' . $fileInfo['fullPath']
        ];
    }

    /**
     * @Author sxy
     * 查看收到的通知公告详情
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function receivedOaNoticeEdit()
    {
        try {
            $request = request()->all();
            $id = $request['id'];
            $oaNotice = new OaNotice();
            $noticeInfo = $oaNotice->getOaNoticeInfo($id);
            return $this->view('received-notice-edit')->with('noticeInfo', $noticeInfo);
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }



    /**
     * 下载附件
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function downloadAttachments()
    {
        $download=request('download');
        return response()->download(public_path($download));
    }








}